package cn.lg.soar.system.biz.modules.auth.service.impl;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.current.CurrentProxy;
import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.system.api.model.UserPostItemVO;
import cn.lg.soar.system.biz.config.SystemCacheKey;
import cn.lg.soar.system.biz.modules.auth.entity.Post;
import cn.lg.soar.system.biz.modules.auth.entity.PostRoleRel;
import cn.lg.soar.system.biz.modules.auth.entity.UserPostRel;
import cn.lg.soar.system.biz.modules.auth.mapper.PostMapper;
import cn.lg.soar.system.biz.modules.auth.model.PostDto;
import cn.lg.soar.system.biz.modules.auth.service.IPostService;
import cn.lg.soar.system.biz.modules.auth.service.PostRoleRelService;
import cn.lg.soar.system.biz.modules.auth.service.PostTagRelService;
import cn.lg.soar.system.biz.modules.auth.service.UserPostRelService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author luguoxiang
 * @date 2021/8/21
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class PostServiceImpl extends BaseServiceImpl<PostMapper, Post> implements IPostService, CurrentProxy<IPostService> {

    @Autowired
    private PostRoleRelService postRoleRelService;
    @Autowired
    private PostTagRelService postTagRelService;
    @Autowired
    private UserPostRelService userPostRelService;

    @Cacheable(value = SystemCacheKey.POSTID_TO_POST, key = "#p0")
    @Override
    public Post getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public PostDto detail(Long id) {
        Post post = proxy().getById(id);
        PostDto dto = new PostDto(post);
        dto.setRoleIds(postRoleRelService.getRoleIds(id));
        dto.setTagIds(postTagRelService.getTagIds(id));
        return dto;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Long create(PostDto data) {
        Post convert = data.toPost();
        AssertUtil.isTrue(this.save(convert), "创建组织机构失败");
        if (DataUtil.isValuable(data.getRoleIds())) {
            postRoleRelService.setPostRole(convert.getId(), data.getRoleIds());
        }
        if (DataUtil.isValuable(data.getTagIds())) {
            postTagRelService.setPostTag(convert.getId(), data.getTagIds());
        }
        return convert.getId();
    }

    @CacheEvict(value = { SystemCacheKey.POSTID_TO_POST}, key = "#p0.id")
    @Override
    public void update(PostDto data) {
        Post convert = data.toPost();
        AssertUtil.isTrue(this.updateById(convert), "创建组织机构失败");
        if (DataUtil.isValuable(data.getRoleIds())) {
            postRoleRelService.setPostRole(convert.getId(), data.getRoleIds());
        }
        if (DataUtil.isValuable(data.getTagIds())) {
            postTagRelService.setPostTag(convert.getId(), data.getTagIds());
        }
        userPostRelService.updatePost(convert.getId());
    }

    @CacheEvict(value = { SystemCacheKey.POSTID_TO_POST}, key = "#p0.id")
    @Override
    public boolean updateById(Post entity) {
        return super.updateById(entity);
    }

    @CacheEvict(value = { SystemCacheKey.POSTID_TO_POST}, key = "#p0")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(value = { SystemCacheKey.POSTID_TO_POST }, allEntries = true)
    @Override
    public void clearCache() {}

    @CacheEvict(value = { SystemCacheKey.POSTID_TO_POST}, allEntries = true)
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean removeByIds(Collection<?> list) {
        userPostRelService.remove(Wrappers.<UserPostRel>lambdaQuery().in(UserPostRel::getPostId, list));
        postRoleRelService.remove(Wrappers.<PostRoleRel>lambdaQuery().in(PostRoleRel::getPostId, list));
        return super.removeByIds(list);
    }

    @Override
    public List<UserPostItemVO> listByUserId(Long userId) {
        return userPostRelService.getPosts(userId);
    }

    @Override
    public UserPostItemVO getByUserId(Long userId) {
        return userPostRelService.getMain(userId);
    }

    @Override
    public List<Long> idsByOrgAndTag(Long orgId, String tagCode) {
        return baseMapper.idsByOrgAndTag(orgId, tagCode);
    }
}
